package com.ruoyi.psi.quality.report.service;

import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.base.supplier.domain.SupplierInfo;
import com.ruoyi.cs.base.supplier.mapper.SupplierInfoMapper;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrder;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrderList;
import com.ruoyi.psi.purchase.order.mapper.PurchaseOrderMapper;
import com.ruoyi.psi.quality.report.domain.InspectReportCond;
import com.ruoyi.psi.quality.report.domain.InspectReportDomain;
import com.ruoyi.psi.quality.report.domain.InspectReportDto;
import com.ruoyi.psi.quality.report.domain.InspectReportList;
import com.ruoyi.psi.quality.report.mapper.InspectReportMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 质检报告
 * 业务层执行
 * @author Drj 2021/6/28
 */
@Service
public class InspectReportServiceImpl implements IInspectReportService {
    @Autowired
    private InspectReportMapper inspectReportMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper; //采购订单
    @Autowired
    private SupplierInfoMapper supplierInfoMapper; //供应商
    @Autowired
    private IdUtil idUtil;

    /**
     * 查询质检报告
     * @param cond 查询条件
     * @return
     */
    @Override
    public List<InspectReportDto> selectInspectReportList(InspectReportCond cond) {
        List<InspectReportDto> inspectReportDtos = inspectReportMapper.selectInspectReportList(cond);
        for (InspectReportDto s : inspectReportDtos) {
            if (s.getSourceType().equals(ERPConstant.RECEIVING_PURCHASE)) {
                // 采购订单
                PurchaseOrder purchaseOrder = purchaseOrderMapper.selectPurchaseOrderById(s.getSourceId());
                SupplierInfo supplierInfo = supplierInfoMapper.selectSupplierInfoById(purchaseOrder.getSupplierInfoId());//查询供应商资料
                s.setSourceName(supplierInfo.getSupplierInfoName()); //设置质检报告的供应商名称
            }
            //子表
            List<InspectReportList> inspectReportLists = inspectReportMapper.selectInspectReportListListById(s.getInspectReportId());
            if (StringUtils.isNotNull(inspectReportLists)) {
                s.setInspectReportLists(inspectReportLists);
            }
        }
        return inspectReportDtos;
    }

    /**
     * 查询对应编号质检报告
     * @param inspectReportId 质检编号
     * @return
     */
    @Override
    public InspectReportDto selectInspectReportById(String inspectReportId) {
        InspectReportDto inspectReport = inspectReportMapper.selectInspectReportById(inspectReportId);
        if (inspectReport.getSourceType().equals(ERPConstant.RECEIVING_PURCHASE)) {
            // 采购订单
            PurchaseOrder purchaseOrder = purchaseOrderMapper.selectPurchaseOrderById(inspectReport.getSourceId());
            SupplierInfo supplierInfo = supplierInfoMapper.selectSupplierInfoById(purchaseOrder.getSupplierInfoId());//查询供应商资料
            inspectReport.setSourceName(supplierInfo.getSupplierInfoName()); //设置质检报告的供应商名称
        }
        //子表
        List<InspectReportList> inspectReportLists = inspectReportMapper.selectInspectReportListListById(inspectReportId);
        if (StringUtils.isNotNull(inspectReportLists)) {
            inspectReport.setInspectReportLists(inspectReportLists);
        }
        return inspectReport;
    }

    /**
     * 新增质检报告
     * @param inspectReport
     * @return
     */
    @Override
    public int insertInspectReport(InspectReportDomain inspectReport) {
        String inspectReportId = idUtil.createInspectReportId();//自动生成编号
        if (StringUtils.isNotNull(inspectReportMapper.selectInspectReportById(inspectReportId))) {
            // 存在
            return 0;
        }
        inspectReport.setInspectReportId(inspectReportId);//主表添加编号
        inspectReportMapper.insertInspectReport(inspectReport);
        List<InspectReportList> inspectReportLists = inspectReport.getInspectReportLists();
        for (InspectReportList sl : inspectReportLists) {
            sl.setInspectReportId(inspectReportId); //子表添加编号
            inspectReportMapper.insertInspectReportList(sl); //子表新增
        }
        // 改变采购订单记录的质检状态
        PurchaseOrderList purchaseOrderList = purchaseOrderMapper.selectPurchaseOrderListById(inspectReport.getSourceId(), inspectReport.getProduceId());
        purchaseOrderList.setInspectStatus(ERPConstant.CHECKED); //将该产品设置为已检
        purchaseOrderMapper.updatePurchaseOrderList(purchaseOrderList); //更新子表
        List<PurchaseOrderList> purchaseOrderLists = purchaseOrderMapper.selectNotInspectPurchaseOrderById(inspectReport.getSourceId()); //查询一下该采购订单是否还有没有质检的子表
        if (purchaseOrderLists.size() == 0) {
            // 说明所有订单产品都已经质检
            PurchaseOrder purchaseOrder = purchaseOrderMapper.selectPurchaseOrderById(inspectReport.getSourceId());
            purchaseOrder.setInspectStatus(ERPConstant.CHECKED); //将该订单设置为已检
            purchaseOrderMapper.updatePurchaseOrder(purchaseOrder); //更新主表
        }
        return 1;
    }

    /**
     * 更新质检报告
     * @param inspectReport
     * @return
     */
    @Override
    public int updateInspectReport(InspectReportDomain inspectReport) {
        if (!StringUtils.isNotNull(inspectReportMapper.selectInspectReportById(inspectReport.getInspectReportId()))) {
            // 不存在
            return 0;
        }
        //存在
        inspectReportMapper.updateInspectReport(inspectReport);
        List<InspectReportList> inspectReportListsOld = inspectReportMapper.selectInspectReportListListById(inspectReport.getInspectReportId());
        List<InspectReportList> inspectReportListsNew = inspectReport.getInspectReportLists();
        for (int i = 0; i < inspectReportListsNew.size(); i++) {
            InspectReportList inspectReportList = inspectReportMapper.selectInspectReportListById(inspectReport.getInspectReportId(), inspectReportListsNew.get(i).getInspectItemId());
            if (StringUtils.isNotNull(inspectReportList)) {
                for (int j = 0; j < inspectReportListsOld.size(); j++) {
                    if (inspectReportListsOld.get(j).getInspectItemId() == inspectReportList.getInspectItemId()) {
                        inspectReportListsOld.remove(j);
                        break;
                    }
                }
                //更新
                inspectReportMapper.updateInspectReportList(inspectReportListsNew.get(i));
            } else {//新增
                inspectReportMapper.insertInspectReportList(inspectReportListsNew.get(i));
            }
        }
        // 说明有删除的对象
        for (int i = 0; i < inspectReportListsOld.size(); i++) {
            //最后将要删除的子标记录删除
            inspectReportMapper.deleteInspectReportListById(inspectReportListsOld.get(i).getInspectReportId(), inspectReportListsOld.get(i).getInspectItemId());
        }
        return 1;
    }

    /**
     * 删除质检报告
     * 仅修改删除标志位
     * @param inspectReport
     * @return
     */
    @Override
    public int updateInspectReportDeleteFlag(InspectReportDomain inspectReport) {
        return inspectReportMapper.updateInspectReportDeleteFlag(inspectReport);
    }

    /**
     * 查询所有详情信息
     * @param cond
     * @return
     */
    @Override
    public List<InspectReportList> selectInspectReportListList(InspectReportCond cond) {
        return inspectReportMapper.selectInspectReportListList(cond);
    }

    /**
     * 查询对应编号的所有质检数据
     * @return
     */
    @Override
    public List<InspectReportList> selectInspectReportListListById(String inspectReportId) {
        return inspectReportMapper.selectInspectReportListListById(inspectReportId);
    }

    /**
     * 查询对应编号质检报告产品信息
     * @param inspectReportId 质检报告编号
     * @param inspectItemId 产品编号
     * @return
     */
    @Override
    public InspectReportList selectInspectReportListById(String inspectReportId, int inspectItemId) {
        return inspectReportMapper.selectInspectReportListById(inspectReportId, inspectItemId);
    }
}

